﻿using HuiZe.education.Resource.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace HuiZe.education.View.AttendClassView
    {
    /// <summary>
    /// WebBrowserOverlay.xaml 的交互逻辑
    /// </summary>
    /// 
    
      public partial class WebVideoPlay : Window
    {
        FrameworkElement _placementTarget;


        public WebBrowser WebBrowser { get; set; }

        //public VideoPlayer2 videoPlayer2 { get { return this.videoPlayer; } }

        public WebVideoPlay(FrameworkElement placementTarget)
        {
            InitializeComponent();

            //_wb = WebBrowser;
            _placementTarget = placementTarget;
            Window owner = Window.GetWindow(placementTarget);
            Debug.Assert(owner != null);
            //Owner = owner;
            //owner.SizeChanged += delegate { OnSizeLocationChanged(); };
            owner.LocationChanged += delegate { OnSizeLocationChanged(); };
            _placementTarget.SizeChanged += delegate { OnSizeLocationChanged(); };


            if (owner.IsVisible)
            {
                Owner = owner;
                //OnSizeLocationChanged();
                Show();
            }
            else
                owner.IsVisibleChanged += delegate
                {
                    if (owner.IsVisible)
                    {
                        Owner = owner;
                        Show();
                    }
                };


            //owner.LayoutUpdated += new EventHandler(OnOwnerLayoutUpdated);
        }
        /// <summary>
        /// 播放控制按钮的点击事件
        /// </summary>
        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            mediaElement.Play();
        }

       
        /// <summary>
        /// 暂停播放控制按钮的点击事件
        /// </summary>
        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            mediaElement.Pause();
        }

        /// <summary>
        /// 停止播放控制按钮的点击事件
        /// </summary>
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            mediaElement.Stop();
        }
        /// <summary>
        /// 移动视频的音量
        /// </summary>
        private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mediaElement.Volume = sliderVolume.Value;
        }
        /// <summary>
        /// 移动控制视频的播放位置
        /// </summary>
        private void sliderPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mediaElement.Position = TimeSpan.FromSeconds(sliderPosition.Value);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            if (!e.Cancel)
                // Delayed call to avoid crash due to Window bug.
                Dispatcher.BeginInvoke((Action)delegate
                {
                    Owner.Close();
                });
        }


        void OnSizeLocationChanged()
        {
            if (Owner != null)
            {
                Point offset = _placementTarget.TranslatePoint(new Point(), Owner);
                Point size = new Point(_placementTarget.ActualWidth, _placementTarget.ActualHeight);
                HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(Owner);
                CompositionTarget ct = hwndSource.CompositionTarget;
                offset = ct.TransformToDevice.Transform(offset);
                size = ct.TransformToDevice.Transform(size);


                Win32.POINT screenLocation = new Win32.POINT(offset);
                Win32.ClientToScreen(hwndSource.Handle, ref screenLocation);
                Win32.POINT screenSize = new Win32.POINT(size);

                //问题：(HwndSource)HwndSource.FromVisual(this)为null
                Win32.MoveWindow(((HwndSource)HwndSource.FromVisual(this)).Handle, screenLocation.X, screenLocation.Y, screenSize.X, screenSize.Y, true);
                //Win32.MoveWindow(((HwndSource)HwndSource.FromVisual(WebBrowserOverlay this)).Handle, screenLocation.X, screenLocation.Y, screenSize.X, screenSize.Y, true);
            }

        }

        /// <summary>
        /// 播放进度监听
        /// </summary>
        private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            //int total = (int)mediaElement.NaturalDuration.TimeSpan.TotalSeconds;
            //sliderPosition.Maximum = total;
            ////格式化数字进度
            //string minutes = String.Format("{0:D2}", (total % 3600) / 60);
            //string seconds = String.Format("{0:D2}", total % 60);
            //textAllNum.Text = minutes + ":" + seconds;
            //if (timer == null)
            //{
            //    timer = new DispatcherTimer();
            //}
            //timer.Interval = TimeSpan.FromSeconds(1);
            //timer.Tick += new EventHandler(timer_tick);
            //timer.Start();
        }
    }
    
}

